﻿Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO

Namespace WPF.Utility

    Public NotInheritable Class Helpers

#Region " Constructors "

        Private Sub New()
        End Sub

#End Region

#Region " Methods "

        Public Shared Function DeepCopy(Of T)(ByVal objToClone As Object) As T
            Using ms As New MemoryStream

                Dim bf As New BinaryFormatter
                bf.Serialize(ms, objToClone)
                ms.Position = 0
                Return CType(bf.Deserialize(ms), T)
            End Using
        End Function

        Public Shared Function FindAncestor(Of parentItem As DependencyObject)(ByVal objDependencyObject As DependencyObject) As parentItem

            While (objDependencyObject) IsNot Nothing
                objDependencyObject = VisualTreeHelper.GetParent(objDependencyObject)

                If TypeOf objDependencyObject Is parentItem Then
                    Return TryCast(objDependencyObject, parentItem)
                End If

            End While

            Return Nothing
        End Function

        Public Shared Function FindVisualChild(Of childItem As DependencyObject)(ByVal obj As DependencyObject) As childItem

            For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(obj) - 1

                Dim child As DependencyObject = VisualTreeHelper.GetChild(obj, i)

                If child IsNot Nothing AndAlso TypeOf child Is childItem Then
                    Return DirectCast(child, childItem)

                Else

                    Dim childOfChild As childItem = FindVisualChild(Of childItem)(child)

                    If childOfChild IsNot Nothing Then
                        Return childOfChild
                    End If

                End If

            Next

            Return Nothing
        End Function

        Public Shared Sub StartProcessWithFileName(ByVal strFileName As String)

            'Start a process :  http://dotnet.mvps.org/dotnet/faqs/?id=openfileappwebpage&lang=en
            Dim psi As New System.Diagnostics.ProcessStartInfo
            psi.FileName = strFileName
            psi.UseShellExecute = True
            System.Diagnostics.Process.Start(psi)
        End Sub

        Public Shared Sub UpdateFocusedField()

            Dim fwE As FrameworkElement = TryCast(Keyboard.FocusedElement, FrameworkElement)

            If fwE IsNot Nothing Then

                Dim expression As BindingExpression = Nothing

                If TypeOf fwE Is TextBox Then
                    expression = fwE.GetBindingExpression(TextBox.TextProperty)
                    '
                    'TODO developers add more controls types here.  Won't be that many.
                    'this would include custom TextBox controls or 3rd party TextBox controls 
                End If

                If expression IsNot Nothing Then
                    expression.UpdateSource()
                End If

            End If

        End Sub

#End Region

    End Class

End Namespace
